package com.liusaidh.controller;

import com.liusaidh.domain.AuditBlog;
import com.liusaidh.domain.MyBlog;
import com.liusaidh.domain.Report;
import com.liusaidh.domain.User;
import com.liusaidh.dto.UpdateFormDTO;
import com.liusaidh.service.BlogService;
import com.liusaidh.service.FansService;
import com.liusaidh.service.MyService;
import com.liusaidh.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

import static com.liusaidh.utils.MysqlConstants.PAGE_BEGIN;
import static com.liusaidh.utils.MysqlConstants.PAGE_SIZE;

@Validated
@Api(tags = "用户个人中心的的业务")
@Controller
@RequestMapping("/my")
@Slf4j
public class MyController {

    @Resource
    private UserService userService;
    @Resource
    private FansService fansService;
    @Resource
    private BlogService blogService;
    @Resource
    private MyService myService;


    @ApiOperation(value = "获取有关举报的信息给被举报的用户", notes = "reportList中放的是有关举报的信息", response = Report.class)
    @GetMapping("/getReportForUser")
    public ModelAndView getReportForUser(HttpSession session) {
        return myService.getReportForUser((Integer) session.getAttribute("id"), PAGE_BEGIN.getState(), PAGE_SIZE.getState());
    }

    @ApiOperation(value = "ajax获取违规通知", notes = "ajax获取将要加载的违规的通知的信息", response = Report.class)
    @GetMapping("/getLoadedReportForUser")
    @ResponseBody
    public List<Report> getLoadedReportForUser(HttpSession session,
                                               @RequestParam(value = "pageNo",defaultValue = "1") @Param(value = "当前的页码数") Integer pageNo,
                                               @RequestParam(value = "pageSize",defaultValue = "10") @Param(value = "每页的数量") Integer pageSize) {
        return myService.getReportListForUser((Integer) session.getAttribute("id"), pageNo, pageSize);
    }


    @ApiOperation(value = "显示个人信息", notes = "跳转到个人信息的页面，显示个人信息，1.返回一个user存放用户的信息，2.返回fansNum：代表粉丝的数量，" +
            "3.返回idolsNum：代表关注的数量4.返回一个myBlog集合，里面放的是AuditBlog对象，代表我的文章5.返回hotBlog集合，里面放的是Blog对象，用于显示右边热搜的标签", response = MyBlog.class)
    @GetMapping("/toMyInfo")
    public ModelAndView getMyInfo(HttpSession session) {
        ModelAndView mav = new ModelAndView("personalPage");
        Integer id = (Integer) session.getAttribute("id");
        List<MyBlog> myBlog = blogService.getAllMyBlog(id, 1, 10);
        List<String> hotBlog = blogService.getTenHotSearchBlog();
        User user = userService.getUserById(id);
        mav.addObject("hotLabelList", blogService.getTenHotSearchBlog());
        mav.addObject("user", user);
        mav.addObject("fansNum", fansService.getMyFansListSize(id));
        mav.addObject("myBlog", myBlog);
        mav.addObject("idolsNum", fansService.getMyIdolsListSize(id));
        return mav;
    }

    @ApiOperation(value = "ajax获取我的文章", notes = "返回list集合，里面是MyBlog对象", response = MyBlog.class)
    @GetMapping("/getLoadedMyBlog")
    @ResponseBody
    public List<MyBlog> getLoadedMyBlog(HttpSession session,
                                        @ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return blogService.getAllMyBlog((Integer) session.getAttribute("id"), pageNo, pageSize);
    }


    @ApiOperation(value = "我的关注", notes = "显示我的关注的信息,返回list集合，集合中有User对象", response = User.class)
    @GetMapping("/toMyIdols")
    public ModelAndView toMyIdol(HttpSession session) {
        ModelAndView mav = new ModelAndView("personalPage-myFocus");
        List<User> list = fansService.getMyIdolsList((Integer) session.getAttribute("id"), 1, 10);
        mav.addObject("list", list);
        mav.addObject("allType", blogService.getTypeByLike(null));
        mav.addObject("hotLabelList", blogService.getTenHotSearchBlog());
        return mav;
    }

    @ApiOperation(value = "ajax获取我的关注的信息", notes = "ajax获取将要加载的我的关注的信息,返回list集合，集合中有User对象", response = User.class)
    @GetMapping("/toMyLoadedIdols")
    @ResponseBody
    public List<User> toMyLoadedIdols(HttpSession session,
                                      @ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return fansService.getMyIdolsList((Integer) session.getAttribute("id"), pageNo, pageSize);
    }

    @ApiOperation(value = "ajax获取我的粉丝的信息", notes = "ajax获取将要加载的我的粉丝的信息,返回list集合，集合中有User对象", response = User.class)
    @GetMapping("/toMyLoadedFans")
    @ResponseBody
    public List<User> toMyLoadedFans(HttpSession session,
                                     @ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return fansService.getMyFansList((Integer) session.getAttribute("id"), pageNo, pageSize);
    }


    @ApiOperation(value = "我的粉丝", notes = "显示我的粉丝的信息,返回list集合，集合中有User对象", response = User.class)
    @GetMapping("/toMyFans")
    public ModelAndView toMyFans(HttpSession session) {
        ModelAndView mav = new ModelAndView("personalPage-myFans");
        Integer id = (Integer) session.getAttribute("id");
        List<User> list = fansService.getMyFansList(id, 1, 10);
        mav.addObject("list", list);
        mav.addObject("allType", blogService.getTypeByLike(null));
        mav.addObject("hotLabelList", blogService.getTenHotSearchBlog());
        return mav;
    }

    @ApiOperation(value = "我的点赞", notes = "显示我点赞的文章,返回list集合，list集合中有AuditBlog对象", response = AuditBlog.class)
    @GetMapping("/toMyLike")
    public ModelAndView toMyLike(HttpSession session) {
        ModelAndView mav = new ModelAndView("personalPage-myLikes");
        Integer id = (Integer) session.getAttribute("id");
        List<AuditBlog> list = blogService.getMyLikeBlogList(id, 1, 10);
        mav.addObject("list", list);
        mav.addObject("allType", blogService.getTypeByLike(null));
        mav.addObject("hotLabelList", blogService.getTenHotSearchBlog());
        return mav;
    }

    @ApiOperation(value = "ajax获取我点赞的文章", notes = "显示我点赞的文章,返回list集合，list集合中有AuditBlog对象", response = AuditBlog.class)
    @GetMapping("/toLoadedMyLike")
    @ResponseBody
    public List<AuditBlog> toLoadedMyLike(HttpSession session,
                                          @ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return blogService.getMyLikeBlogList((Integer) session.getAttribute("id"), pageNo, pageSize);
    }

    @ApiOperation(value = "我的收藏", notes = "显示我收藏的文章,返回list集合，list集合中有AuditBlog对象", response = AuditBlog.class)
    @GetMapping("/toMyCollection")
    public ModelAndView toMyCollection(HttpSession session) {
        ModelAndView mav = new ModelAndView("personalPage-myFavs");
        Integer id = (Integer) session.getAttribute("id");
        List<AuditBlog> list = blogService.getMyCollectionBlogList(id, 1, 10);
        mav.addObject("list", list);
        mav.addObject("allType", blogService.getTypeByLike(null));
        mav.addObject("hotLabelList", blogService.getTenHotSearchBlog());
        return mav;
    }

    @ApiOperation(value = "ajax获取我收藏的文章", notes = "显示我收藏的文章,返回list集合，list集合中有AuditBlog对象", response = AuditBlog.class)
    @GetMapping("/toLoadedMyCollection")
    @ResponseBody
    public List<AuditBlog> toLoadedMyCollection(HttpSession session,
                                                @ApiParam(name = "pageNo", value = "当前的页码数（及当前是第几页，前端传参）,默认展示第一页") @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @ApiParam(name = "pageSize", value = "每页的数量（及每页展示几条数据，前端传参），默认是十条") @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return blogService.getMyLikeBlogList((Integer) session.getAttribute("id"), pageNo, pageSize);
    }


    @ApiOperation(value = "修改时，先回显我的个人信息", response = User.class)
    @GetMapping(value = "/echoMyMessage")
    public ModelAndView echoMyMessage(HttpSession session) {
        return myService.echoMyMessage(session);
    }

    @ApiOperation(value = "修改个人信息")
    @PostMapping(value = "/toSetUpMyInfo")
    public ModelAndView toSetUpMyInfo(HttpSession session,
                                      @Valid @ApiParam(value = "用户的其他信息") UpdateFormDTO userDTO,
                                      @ApiParam(value = "用户的头像") @RequestParam(name = "file",required = false) MultipartFile file) throws Exception {
        return myService.updateMyInfo(session, userDTO, file);
    }

    @ApiOperation(value = "修改密码", notes = "修改我的密码")
    @PostMapping(value = "/toUpdateMyPwd")
    public ModelAndView toUpdateMyPwd(HttpSession session,
                                      @ApiParam(name = "oldPwd", value = "原来的密码") @RequestParam(name = "oldPwd") String oldPwd,
                                      @ApiParam(name = "newPwd", value = "新的密码") @RequestParam(name = "newPwd") String newPwd,
                                      @ApiParam(name = "confirmPwd", value = "密码验证") @RequestParam(name = "confirmPwd") String confirmPwd) {
        return myService.updateMyPassword(session, oldPwd, newPwd, confirmPwd);
    }

    @ApiOperation(value = "我的草稿箱文章", notes = "显示我的草稿箱,1.返回list集合,放有一个个的AuditBlog对象 2.返回一个user，是一个User对象，放有当前登录用户的信息", response = AuditBlog.class)
    @GetMapping(value = "/getMyDraftBox")
    public ModelAndView getMyDraftBox(HttpSession session) {
        ModelAndView mav = new ModelAndView("home");
        mav.addObject("allType", blogService.getTypeByLike(null));
        Integer id = (Integer) session.getAttribute("id");
        List<AuditBlog> list = blogService.getMyDraftBox(id);
        User user = userService.getUserById((Integer) session.getAttribute("id"));
        mav.addObject("user", user);
        mav.addObject("list", list);
        return mav;
    }


}